package com.example.yskjproject.utils;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.example.yskjproject.content.PushConstants;
import com.example.yskjproject.helper.PushHelper;
import com.lzy.okgo.OkGo;
import com.tencent.mmkv.MMKV;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.PushAgent;
import com.umeng.message.api.UPushRegisterCallback;


public class AppInitializer {
    private Application context;

    public AppInitializer(Application context) {
        this.context = context;
    }

    public AppInitializer initUPush() {
        //友盟日志输出开关
        UMConfigure.setLogEnabled(true);
        //预初始化
        UMConfigure.preInit(context, PushConstants.APP_KEY, PushConstants.CHANNEL);
        //建议在子线程中初始化
        new Thread(() -> init(context)).start();

        return this;
    }

    public static void init(Context context) {
        // 初始化配置，应用配置信息：http://message.umeng.com/list/apps
        // 参数1：上下文context；
        // 参数2：应用申请的Appkey；
        // 参数3：发布渠道名称；
        // 参数4：设备类型，UMConfigure.DEVICE_TYPE_PHONE：手机；UMConfigure.DEVICE_TYPE_BOX：盒子；默认为手机
        // 参数5：Push推送业务的secret，填写Umeng Message Secret对应信息
        UMConfigure.init(context, PushConstants.APP_KEY, PushConstants.CHANNEL, UMConfigure.DEVICE_TYPE_PHONE, PushConstants.MESSAGE_SECRET);

        PushAgent api = PushAgent.getInstance(context);
        PushHelper.setting(api);
        // 推送注册
        api.register(new UPushRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                Log.e("yment :", "deviceToken: " + deviceToken);
                if (!TextUtils.isEmpty(deviceToken)) {
                    SpCashUtils.getInstance().setDeviceToken(deviceToken);
                }
            }

            @Override
            public void onFailure(String errCode, String errDesc) {
                Log.e("yment :", "register failed! " + "code:" + errCode + ",desc:" + errDesc);
            }
        });
    }

    public AppInitializer initSP() {
        MMKV.initialize(context);
        return this;
    }

    public AppInitializer initOkGo() {
        OkGo.getInstance().init(context);
        return this;
    }



}
